package cq.com.cn.servlet;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import cq.com.cn.model.BooksModel;
import cq.com.cn.service.BooksService;
import cq.com.cn.utils.AjaxUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/query/findBook.do")
public class QueryBookServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //参数处理
       String typeParams=req.getParameter("type");
       int type=typeParams==null?0:Integer.parseInt(typeParams);
       //业务处理
       BooksService booksService=new BooksService();
       List<BooksModel> booksList = booksService.queryBooksByType(type);

       //数据返回到页面
        String books = new Gson().toJson(booksList);
        AjaxUtils.ajaxHtml(books,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
